#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;
using db = double;
using tl = tuple < int, int, int >;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 5e5 + 10;
const int mod = 1e9 + 7;

int n, a[N], b[N], c[N];
int ans;

inline int upd(const int &x) { return x + (x >> 31 & mod); }

namespace BIT {
	int tr[N];
	void clear() { memset(tr, 0, sizeof tr); }
	void upd(int x, const int &v) { for(; x <= n; x += x & -x) tr[x] = ::upd(tr[x] + v - mod); }
	int query(int x) { int res = 0; for(; x; x -= x & -x) res = ::upd(res + tr[x] - mod); return res; }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
	n = in; rep(i, 1, n) a[i] = b[i] = in;
	sort(b + 1, b + n + 1);
	rep(i, 1, n) c[i] = lower_bound(b + 1, b + n + 1, a[i]) - b;
	rep(i, 1, n) {
		BIT :: upd(c[i], i);
		int res = 1ll * BIT :: query(c[i]) * (n - i + 1) % mod * a[i] % mod;
		ans = upd(ans + res - mod);
	}
	BIT :: clear();
	per(i, n, 1) {
		int res = 1ll * BIT :: query(c[i]) * i % mod * a[i] % mod;
		BIT :: upd(c[i], n - i + 1);
		ans = upd(ans + res - mod);
	} printf("%d\n", ans); return 0;
}
